<?php

/**
* alltosun.com  index.php for newbd
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* Author: 吴松键 (wusj@alltosun.com)
* Date: 15/1/20 下午3:53
*/

class Action
{
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $search_filter = Request::getParam('search_filter', array());

        $filter = array(
            'page_no' => $page,
            'status'  => $status,
            'title'   => @$search_filter['title']
        );

        $goods_list = _widget('promotion')->get_list($filter);

        Response::assign('goods_list', $goods_list);

        Response::display('admin/list.html');
    }

    public function add_goods()
    {
        $goods_ids   = explode(',', Request::getParam('goods_ids', 0));
        $sale_prices = explode(',', Request::getParam('price_arr', 0));

        if (!$goods_ids) {
            return '请选择商品';
        }
        if (!$sale_prices) {
            return '请输入优惠价格';
        }
        foreach ($goods_ids as $k=>$v) {
            if ($v) {
                $info = array(
                    'goods_id' => $v,
                    'sale_price' => $sale_prices[$k]
                );
                _model('promotion_add')->create($info);
            }
        }

        return 'ok';
    }

    public function eidt_goods()
    {
        $id = Request::getParam('id', 0);
        $sale_price = Request::getParam('sale_price', 0);

        if (!$id) {
            return '请选择需要修改的商品';
        }

        _model('promotion_add')->update(array('id'=>$id), array('sale_price'=>$sale_price));

        return array('info'=>'ok', 'sale_price'=>number_format($sale_price, 2));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $promotion_info = _uri('promotion_add', (int)$v);
            if (!$promotion_info) {
                continue;
            }

            _model('promotion_add')->delete($v);
        }

        return 'ok';
    }

    /**
     * ajax 获取关联商品信息
     */
    public function get_goods_list()
    {
        $pid = Request::getParam('pid', 0);

        if ($pid < 0) {
            return array('info'=>'ok', 'data'=>'');
        }

        $list = array();
        $list = _model('category')->getList(array('parent_id'=>$pid, 'res_name'=>'goods', 'status'=>1));

        if ($list) {
            $method = 'category';
        } else {
            $method = 'goods';
            $filter = array(
                'category_id'   => $pid,
                'status'        => 1,
                '`id` <>' => _model('promotion_add')->getFields('goods_id', array('status'=>1))
            );
            $count = _model('goods')->getTotal($filter);
            if (!$count) {
                return array('info'=>'ok', 'data'=>'<div style="padding:10px;">该分类下没有商品</div>');
            }

            $list = _model('goods')->getList($filter);
        }
        Response::assign('method', $method);
        Response::assign('list', $list);
        $data = Response::fetch(MODULE_PATH.'/promotion/template/admin/slt_goods.html');

        return array('info'=>'ok', 'data'=>$data);
    }
}